<% layout('/layouts/default.html', {title: '指标汇总', libs: ['layout','dataGrid','inputmask','ueditor']}){ %>
<div class="ui-layout-west">
    <div class="main-content">
        <div class="box box-main">
            <div class="box-header" style="padding-bottom: 4px;">
                <div class="box-title">
                    <i class="fa ">	<a href="#"  class="btn btn-primary" onclick="addGroup()"    style="" title="${text("新增常用组")}">新增常用组</a>
                    </i>
                </div>
            </div>
            <#form:hidden path="versionNumber" id="mainVersion"/>
            <div class="box-body">
                <div class="input-group">
                    <#form:input path="taskDesc" maxlength="100" class="form-control " placeholder="请输入常用组名称" autocomplete="off"/>
                    <span class="input-group-addon" onclick="searchTaskDesc()" style="cursor: pointer">查询</span>
                    <span class="input-group-addon" onclick="reset()" style="cursor: pointer">重置</span>
                </div>
            </div>
            <div class="ui-layout-content">
                <table id="leftDataGrid"></table>
            </div>
        </div>
    </div>
</div>
<div class="ui-layout-center">
    <iframe id="mainFrame" name="mainFrame" class="ui-layout-content p0"></iframe>
</div>
<% } %>
<style>
    .ui-layout-content{
        overflow: hidden;
    }
</style>
<script>
    // 初始化布局
    $('body').layout({
        west__initClosed: $(window).width() <= 767, // 是否默认关闭
        west__size: 280
    });
    var win = $("#mainFrame")[0].contentWindow;
    $('#leftDataGrid').dataGrid({
        data: ${toJson(assessmentUnit)},
        datatype: "local", // 设置本地数据
        sortableColumn:false, // 禁止排序
        autoGridHeight: function(){
            var height = $(window).height()-170;
            return height;
        }, // 设置自动高度
        columnModel: [
            {header:'${text("常用组id")}', name:'id',hidden:true, width:150, align:"left"},
            {header:'${text("常用组名称")}', name:'groupName', width:150, align:"left",formatter: function(val, obj, row, act){
                    return row.groupName+'<a href="#" onclick=deleteGroup("'+row.id+'")  style="float:right" ><i class="fa fa-trash-o"></i></a>';
                },unformat: function(val, obj, cell){
                    return $('#'+obj.rowId+'_groupName').val();
                }},
        ],
        emptyDataHint: true,
        // 选择行后调用（id:行id，isSelect：是否是选中，event：事件）
        onSelectRow: function(id, isSelect, event) {
            let selectRow = $('#leftDataGrid').dataGrid('getRowData', id);
            $('#mainFrame').attr('src', '${ctx}/task/dcSupTaskGroupUnit/list?groupId='+selectRow.id);
        },
        // 加载成功默认设置选择第一个
        ajaxSuccess: function(data){
            if (data.total>0){
                let row=data.rows[0];
                $('#mainFrame').attr('src', '${ctx}/task/dcSupTaskGroupUnit/list?groupId='+row.id);
            }
        }
    });

    // 任务信息的onchange事件
    function searchTaskDesc() {
        // 获取用户输入查询项
        let tempTaskDesc = $("#taskDesc").val();
        if(tempTaskDesc != '' && tempTaskDesc != undefined){
            // 记录目标值
            let targetList = [];
            // 遍历表格数据，获取目标值
            let tempList = ${toJson(assessmentUnit)}
            for(let i = 0; i < tempList.length; i++){
                if(tempList[i].groupName.indexOf(tempTaskDesc) != -1){
                    targetList.push(tempList[i]);
                }
            }
            // 刷新表格数据
            $('#leftDataGrid').dataGrid('setParam', {
                data:targetList
            }, true).dataGrid('reloadGrid');
        }
    }

    // 重置表格数据
    function reset() {
        // 重置查询项
        $("#taskDesc").val('');
        // 重置表格数据
        $('#leftDataGrid').dataGrid('setParam', {
            data:${toJson(assessmentUnit)}
        }, true).dataGrid('reloadGrid');
    }

    function addGroup(){
        js.layer.open({
            type: 2,
            title: ['新增常用组'],
            area: ['600px', '300px'],
            scrollbar: true,
            shadeClose: false,
            content: "${ctx}/task/dcSupTaskGroup/form",
            btn1: function (index, layero) {
            },
            end:function () {
                location.reload();
            }
        });

    }

    function deleteGroup(groupId){
        layer.open({
            type: 1
            ,title: ['删除常用组', 'font-size:18px; color:orange;'] //不显示标题栏
            ,btn: ['确认', '取消']
            ,btnAlign: 'c'
            ,area: '500px;'
            ,moveType: 1 //拖拽模式，0或者1
            ,content: "<div style=\"padding: 50px\">您确定删除该常用组吗</div>"

            ,yes: function(layero){

                $.ajax({
                    url: "${ctx}/task/dcSupTaskGroup/delete?id="+groupId,
                    type: "post",
                    cache: false,
                    async:false,
                    success: function(data){

                        location.reload();
                    }
                });


            }
        });
    }

</script>
